home *** CD-ROM | disk | FTP | other *** search
- Path: gate.net!pslfl2-47
- From: bhutto@gate.net (William Hutto)
- Newsgroups: comp.lang.c
- Subject: Re: gets() question
- Date: 9 Jan 1996 04:01:31 GMT
- Organization: CyberGate, Inc.
- Message-ID: <4cspar$1rdi@news.gate.net>
- References: <4cosgf$rir@newsbf02.news.aol.com> <4cqkt8$1quo@news.gate.net> <821109116snz@genesis.demon.co.uk>
- NNTP-Posting-Host: pslfl2-47.gate.net
- X-Newsreader: News Xpress Version 1.0 Beta #4
-
- In article <821109116snz@genesis.demon.co.uk>,
- Lawrence Kirby <fred@genesis.demon.co.uk> wrote:
- >In article <4cqkt8$1quo@news.gate.net> bhutto@gate.net "William Hutto"
- writes:
- >
- >>In article <4cosgf$rir@newsbf02.news.aol.com>,
- >
- >>>#include "stdio.h"
- >
- >Should be:
- >
- >#include <stdio.h>
- >
- >if you want to be sure to include the standard system stdio.h header.
- >
- >>>void main(void)
- >
- >Read the FAQ.
- >
- >>>{
- >>> int i[5];
- >>> char s[3][10];
- >>>
- >>> printf ("Enter s[0]: ");
- >>> gets (s[0]);
- >> ^^^^
- >>
- >>Your compiler didn't generate an error?
- >
- >Why should it?
- >
- >>*s* is not an array of pointers to
- >>arrays of chars as in argv[].
- >
- >True, s is an array of arrays of char. s[0] is an array of 10 chars.
- >When used as an rvalue in an expression an array evaluates to a pointer to
- >its first element i.e. a char * in this case.
- >
- >>Now I remember why I always like being explicit.
- >>Try this:
- >>
- >> fgets(&s[0][0],9,stdin);
- >
- >It is certainly better to use fgets rather than gets but you could have
- >written:
- >
- > fgets(s[0],sizeof s[0],stdin);
- >
- >s[0] is equivalent to &s[0][0] in this context and is arguably more
- >readable. The length argument of fgets specifies the size of the array
- >available in the first argument (which is 10 in this case) i.e. fgets will
- >read a maximum of N-1 characters from the input stream and then terminate the
- >sequence in the array with '\0'.
- >
- >>You can use a 2 dimensional char array, except that you have to be complete
- >>in your form and s[0] is not. Because fgets() (or gets() for that matter) is
- >>looking for an address, you need to oblige, hence the prefix of *&*.
- >
- >You need to read up on how arrays and pointers work in C. There are no such
- >thing as 2 dimensional arrays, just arrays of arrays. Each element of the
- >overall array is itself an array and behaves just like any other array.
- >
-
- One thing I liked about C was it's predictability. :) That was one little
- quirk that I didn't remember. I tend to be explicit in the use of subscripts
- or dereferencing, and so if I had knowledge of that, it must have slipped into
- the bit bucket. I appreciate the clarification guys, and sorry for the
- erroneous data.
-
- Bill
-
- "Whatcha got on?...Your mind?"
-